<?php
namespace App\Http\Service;

use Crypt;
use Illuminate\Http\Request;
use App\Http\Requests\AdminRequest;
use App\Models\Admin;
use App\Models\LoginDetaile;
use App\Http\Service\CommentService;
use Illuminate\Support\Facades\Cookie;
use App\Models\AdminRole;

class LoginService
{
	private static $_obj;
	private $_comment;

	public static function getInstance()
	{
		if (!self::$_obj instanceof self) {
			self::$_obj = new self();
		}
		return self::$_obj;
	}

	public function __construct()
	{
		$this->_comment = CommentService::getInstance();
	}

	public function adminLogin(Request $request, $data)
	{
		$number = $data['number'];
		$list = Admin::where('number', '=', $number)->first();
		$lists = clone $list;
		if ($list) {
			try{
				$password = Crypt::decrypt($list->password);
			} catch (DecryptException $e) {
				return $this->_comment->returnArray(500, '密码解析错误,联系超级管理员');
			}
			if ($data['password'] != $password) {
				$loginDetaile = LoginDetaile::getInstance();
				$loginDetaile->adid = $list->id;
				$loginDetaile->ip = $request->ip();
				$loginDetaile->time = time();
				$loginDetaile->status = 2;
				$loginDetaile->save();

				$count = LoginDetaile::where([
					['time', '>', time()-1800],
					['id', '=', $list->id],
					['status', '', 2]
				])->count();
				if ($count > 10) {
					$LoginStatus = Admin::find($list->id)
						->update(['status'=>3]);
					return $this->_comment->returnArr(301, '您在30分钟内多次密码错误,账户已被锁定');
				}
				return $this->_comment->returnArr(305, '账号或密码错误');
			}else{
				switch ($list->status) {
					case 1:
						session(['fyback_info' => $lists->toArray()]);
						$sessions = $request->session()->getId();
						Admin::find($list->id)->update(['sessionid'=>$sessions]);
						$loginDetaile = LoginDetaile::getInstance();
						$loginDetaile->adid = $list->id;
						$loginDetaile->ip = $request->ip();
						$loginDetaile->time = time();
						$loginDetaile->status = 1;
						$loginDetaile->save();
						if ($data['checkbox'] == 1) {
							Cookie::queue('fid', $list->id, 60*60*24*15);
						}

						if ($list->id != 1) {
							$this->saveNodeToSession($list->id);
						}

						return $this->_comment->returnArr(200, '登陆成功');
						break;
					case 2:
						return $this->_comment->returnArr(301, '您的账户已被锁定');
						break;
					case 3:
						return $this->_comment->returnArr(301, '您的账户已被禁用');
						break;
					
					default:
						return $this->_comment->returnArr(501, '您的账户状态异常,已被禁止登陆'); 
						break;
				}
			}
		}else{
			return $this->_comment->returnArr(305, '账号或密码错误');
		}
	}

	public function getFYBackData(Request $request, $id=0)
	{
		$list = Admin::find($id)->toArray();
		if ($list) {
			switch ($list['status']) {
				case 1:
					session(['fyback_info' => $list]);
					$sessions = $request->session()->getId();
					Admin::find($list['id'])->update(['sessionid'=>$sessions]);
					return $this->_comment->returnArr(200, '登陆成功');
					break;
				case 2:
					return $this->_comment->returnArr(301, '您的账户已被锁定');
					break;
				case 3:
					return $this->_comment->returnArr(301, '您的账户已被禁用');
					break;
				
				default:
					return $this->_comment->returnArr(501, '您的账户状态异常,已被禁止登陆'); 
					break;
			}
		} else {
			return $this->_comment->returnArr(400, '未找到该用户');
		}
	}

	public function getLastLoginDetail()
	{
		$id = session('fyback_info')['id'];
		$data = LoginDetaile::where([['adid',$id],['status',1]])->first()->toArray();
		$lastData = LoginDetaile::where(['adid',$id],['id','<',$data['id']],['status',1])->first();
		if ($lastData) {
			
		}
		dd($data);
	}

	/**
	 * 存入权限到SESSION
	 * @param int $id 管理员唯一标识
	 */
	private function saveNodeToSession($id)
	{
		session('accessRights', [
			'Index'=>['index'],
		]);  //每个人都有访问首页的权限
		//通过aid获取角色
		$node = AdminRole::getInstance()->getRoleListByAid($id);
		$res = [];
		if (!$node->isEmpty()) {
			foreach ($node as $val) {
				$res[$val->mname."Controller"][] = $val->aname;
			}
		}
		if ($res) {
			session('accessRights', $res);
		}
	}
}